import Toast from 'tdesign-miniprogram/toast/index';

const shortageImg = 'https://cdn-we-retail.ym.tencent.com/miniapp/cart/shortage.png';

Component({
  isSpecsTap: false, // 标记本次点击事件是否因为点击specs触发（由于底层goods-card组件没有catch specs点击事件，只能在此处加状态来避免点击specs时触发跳转商品详情）
  externalClasses: ['wr-class'],
  properties: {
    GoodsList: { type: Array },
    InvalidGoods: { type: Array },
    thumbWidth: { type: null },
    thumbHeight: { type: null },
  },

  data: {
    shortageImg,
    currentGoods: {},
    _storeGoods: [],
    _invalidGoodItems: [],
  },

  methods: {
    // 删除商品
    deleteGoods(e) {
      const { goods } = e.currentTarget.dataset;
      this.triggerEvent('delete', { goods });
    },

    // 清空失效商品
    clearInvalidGoods() {
      this.triggerEvent('clearinvalidgoods');
    },

    // 选中商品
    selectGoods(e) {
      const { goods, index } = e.currentTarget.dataset;
      this.triggerEvent('selectgoods', {
        goods,
        isSelected: !goods.isSelected,
        goodsIndex: index
      });
    },

    /* 数量传父组件 */
    changeQuantity(num, goods, goodsIndex) {
      this.triggerEvent('changequantity', {
        goods,
        quantity: num,
        goodsIndex
      });
    },
    /* 监听步进器 */
    changeStepper(e) {
      const { value } = e.detail;
      const { goods, index } = e.currentTarget.dataset;
      this.changeQuantity(value, goods, index);
    },

    input(e) {
      const { value } = e.detail;
      const { goods } = e.currentTarget.dataset;
      const num = value;
      this.changeQuantity(num, goods);
    },
    /* 步进器数量校验 */
    overlimit(e) {
      const text =
        e.detail.type === 'minus'
          ? '该商品不能减少了哦'
          : '商品最多购买999件';
      Toast({
        context: this,
        selector: '#t-toast',
        message: text,
      });
    },

    goGoodsDetail(e) {
      if (this.isSpecsTap) {
        this.isSpecsTap = false;
        return;
      }
      const { goods } = e.currentTarget.dataset;
      this.triggerEvent('goodsclick', { goods });
    },
  },
});
